// pages/me/pages/userRegister/userRegister.js
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    opacity:0.6,
    focus:false,
    inputTelValue: '',
    showCancel:false,
    disabled:true,
    dialogShow:false,
    placeText:'你的手机号',
    isShow:true,
    btnText:'注册',
    isRegister: 0,// 1=找回密码  2=更换手机号  0=注册
    isExist:false,
    isText:false,
    curTel:'',
    flag: 1  //1=注册  2=重置密码  4=修改手机号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      console.log(options)
      if(options.isRegister==1){
        wx.setNavigationBarTitle({
          title: '找回密码'
        })
        this.setData({
          placeText:"输入注册时使用的手机号",
          isShow: false,
          btnText: '下一步',
          isRegister: 1,
          isText: false,
          flag:2
        })
      } else if (options.isRegister==2){
        wx.setNavigationBarTitle({
          title: '更换手机号'
        })
        this.setData({
          placeText: "输入要更换的手机号",
          isShow: false,
          btnText: '更换',
          isRegister: 2,
          isText: true,
          curTel:options.telephone,
          flag: 4
        })
      }
      else{
        wx.setNavigationBarTitle({
          title: '注册'
        })
        this.setData({
          placeText: "你的手机号",
          isShow: true,
          btnText: '注册',
          isRegister: 0,
          isText: false,
          flag: 1
        })
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //
  bindButtonTap:function(){
    this.setData({
      focus: true,
    })
  },
  inputPhoneNum: function (e) {
    this.setData({
      inputTelValue: e.detail.value,
      showCancel: true,
      disabled: false
    })
  },
  // 手机号处取消
  bindCancelbtn: function (e) {
    console.log(e)
    this.setData({
      inputTelValue: '',
      showCancel: false,
      disabled: true,
      opacity: 0.6
    })
  },
  //
  bindRegisterBtn:function(){
    var that=this;
    if (!this.data.inputTelValue) {
      wx.showToast({
        title: '请输入手机号',
        icon: "none",
        duration: 2000
      })
      return;
    } else {
      var reg = /^1[0-9]{10}$/;
      var flag1 = reg.test(this.data.inputTelValue);
      if (flag1 == false) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: "none",
          duration: 2000
        })
        return;
      }
      if (that.data.isRegister == 2){
          if(that.data.curTel == that.data.inputTelValue){
            wx.showToast({
              title: '该手机号与当前手机号相同，请重新输入',
              icon: "none",
              duration: 2000
            })
            return;
          }
      }
      // 验证手机号是否注册过
      wx.request({
        url: 'https://papi.zcyun.cn/v1/user/valid?value=' + that.data.inputTelValue + '&platformId=' + app.globalData.platformId +'&type=telephone',
        method:'GET',
        data: {
          type:'telephone',
          value: that.data.inputTelValue,
          platformId: app.globalData.platformId
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res.data);
          if(res.data.code==0){
            that.setData({
              dialogShow: true
            })
          }else{
            if(res.data.code=='20039'){
              that.setData({
                isExist:true
              })
            }
            if(that.data.isRegister==1){
              that.setData({
                dialogShow: true
              })
            } else if (that.data.isRegister == 2){
              that.setData({
                dialogShow: true
              })
            }else{
              wx.showToast({
                title: '该手机号已被注册，可直接登录',
                icon: "none",
                duration: 2000
              })
            }
          }
        }
      })
    }
  },
  bindXieyiBtn:function(){
    wx:wx.navigateTo({
      url: '',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 弹框操作
  bindCancelBtn:function(){
    this.setData({
      dialogShow:false
    })
  },
  // 确认手机号  确定
  bindConfirmBtn:function(){
    var that=this;
    this.setData({
      dialogShow: true
    })
    if(that.data.isRegister==1){
      if(that.data.isExist){
        that.sendCodeFunc()
      }else{
        wx.showToast({
          title: "用户不存在",
          icon: "none",
          duration: 2000
        })
        that.setData({
          dialogShow:false
        })
      }
    }else{
      that.sendCodeFunc() //调取发送验证码函数
    }
  },
  // 发送验证码
  sendCodeFunc:function(){
    var that=this;
    wx.request({
      url: 'https://papi.zcyun.cn/v1/validCode/send',
      data: {
        value: that.data.inputTelValue,
        sendMode: 2,
        flag: that.data.flag,
        platformId: app.globalData.platformId
      },
      header: {},
      method: 'POST',
      success: function (res) {
        console.log(res.data);
        if (res.data.code == 0) {
          // 进入填写验证码界面
          wx.navigateTo({
            url: '../../verificationCode/verificationCode?telephone=' + that.data.inputTelValue + '&isRegister=' + that.data.isRegister,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "none",
            duration: 2000
          })
        }

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})